package listbyorder.access101_200.test173;

import listbyorder.utils.TreeNode;

import java.util.Stack;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/14 14:35
 */
public class Solution1 {

    // 二叉树的搜索迭代器
    class BSTIterator {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = null;

        public BSTIterator(TreeNode root) {
            cur = root;
        }

        public int next() {
            int res = -1;
            while (cur != null || !stack.isEmpty()) {
                while (cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                }
                // 走到这里节点为null；
                cur = stack.pop();
                res = cur.val;
                cur = cur.right;
                break;
            }
            return res;
        }

        public boolean hasNext() {
            return (cur != null || !stack.isEmpty());
        }
    }
}
